//
//  NSString+ridHTMLStr.m
//  Stars
//
//  Created by hst on 16/3/18.
//  Copyright © 2016年 hst. All rights reserved.
//

#import "NSString+ridHTMLStr.h"
#import "NSString+FXCategory.h"

@implementation NSString (ridHTMLStr)

- (NSString *)replaceAllMark{

    NSString *s = [self copy];
    s = s.replaceSitgMark;
    s = s.replaceSpacesMark;
    s = s.removeWhitespaceCharacterSetAndNewLine;
    return s;
}

-(NSString *) stringByStrippingHTML {
    NSRange r;
    NSString *s = [self copy];

    s = [self ridStylemark:s];
    s = s.replaceSitgMark.replaceSpacesMark.removeWhitespaceCharacterSetAndNewLine;

    while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
        s = [s stringByReplacingCharactersInRange:r withString:@""];
    
    while ((r = [s rangeOfString:@"‍" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@" "];
    }
    while ((r = [s rangeOfString:@"\n\t" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\n"];
    }
    while ((r = [s rangeOfString:@"\r\n" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\n"];
    }
    while ((r = [s rangeOfString:@"\t" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@""];
    }
    while ((r = [s rangeOfString:@"\n\n" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\n"];
    }
    while ((r = [s rangeOfString:@"&ldquo;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\""];
    }
    while ((r = [s rangeOfString:@"&rdquo;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\""];
    }
    while ((r = [s rangeOfString:@"&quot;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\""];
    }
    while ((r = [s rangeOfString:@"&#39;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\'"];
    }
    while ((r = [s rangeOfString:@"&lsquo;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\'"];
    }
    while ((r = [s rangeOfString:@"&rsquo;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\'"];
    }
    while ((r = [s rangeOfString:@"&lt;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"<"];
    }
    while ((r = [s rangeOfString:@"&gt;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@">"];
    }
    while ((r = [s rangeOfString:@"&hellip;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"..."];
    }
    while ((r = [s rangeOfString:@"&amp;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"&"];
    }
    
    while ((r = [s rangeOfString:@"&middot;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"·"];
    }
    
    return s;
}
//剔除 style 标签所有内容
-(NSString *)ridStylemark:(NSString *)html {
    NSScanner *theScanner;
    NSString *text = nil;
    theScanner = [NSScanner scannerWithString:html];
    while ([theScanner isAtEnd] == NO) {
        // find start of tag
        [theScanner scanUpToString:@"<style" intoString:NULL] ;
        // find end of tag
        [theScanner scanUpToString:@"/style>" intoString:&text] ;
        // replace the found tag with a space
        //(you can filter multi-spaces out later if you wish)
        html=[html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@/style>", text] withString:@""];
    }
    return html;
}

-(NSString *)replaceSpacesMark{

    NSString *str = [self copy];
    NSRange r;

    while ((r = [str rangeOfString:@"&nbsp;" options:NSRegularExpressionSearch]).location != NSNotFound){
        str = [str stringByReplacingCharactersInRange:r withString:@" "];
    }
    while ((r = [str rangeOfString:@"<br />" options:NSRegularExpressionSearch]).location != NSNotFound) {
        str = [str stringByReplacingCharactersInRange:r withString:@"\n"];
    }
    while ((r = [str rangeOfString:@"<br/>" options:NSRegularExpressionSearch]).location != NSNotFound) {
        str = [str stringByReplacingCharactersInRange:r withString:@"\n"];
    }
    while ((r = [str rangeOfString:@"<br>" options:NSRegularExpressionSearch]).location != NSNotFound) {
        str = [str stringByReplacingCharactersInRange:r withString:@"\n"];
    }

    return str;
}

- (NSString *)replaceSitgMark{
    NSString *str = [self copy];
    NSRange r;
    while ((r = [str rangeOfString:@"<sitg>" options:NSRegularExpressionSearch]).location != NSNotFound) {
        str = [str stringByReplacingCharactersInRange:r withString:@"("];
    }
    while ((r = [str rangeOfString:@"</sitg>" options:NSRegularExpressionSearch]).location != NSNotFound) {
        str = [str stringByReplacingCharactersInRange:r withString:@")"];
    }
    return str;
}

-(NSString *)getHTMLContentwithStartElement:(NSString *)element1 endElement:(NSString *)element2{
    NSMutableString *text=[NSMutableString stringWithString:self];
    NSRange range11;
    NSRange range12;
    NSString *myContent=@"";
    
    range11=NSMakeRange(0,0);
    range12=NSMakeRange(0,0);
    range11=[text  rangeOfString:element1 options:NSRegularExpressionSearch];
    if (range11.length>0) {
        range12=[text  rangeOfString:element2 options:NSRegularExpressionSearch];
        if (range12.length>0) {
            NSRange range=NSMakeRange(range11.location+range11.length, range12.location-range11.location-range11.length);
            myContent=[text substringWithRange:range];
            [text deleteCharactersInRange:range];
        }
        
    }
    return myContent;
}


// sunxb add
- (NSString *)report_handleHTML {
    NSRange r;
    NSString *s = [self copy];
    
    s = [self ridStylemark:s];
    s = s.replaceSitgMark.replaceSpacesMark;
    
    while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
        s = [s stringByReplacingCharactersInRange:r withString:@""];
    
    while ((r = [s rangeOfString:@"‍" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@" "];
    }
    while ((r = [s rangeOfString:@"\n\t" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\n"];
    }
    while ((r = [s rangeOfString:@"\t" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@""];
    }
    while ((r = [s rangeOfString:@"&ldquo;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\""];
    }
    while ((r = [s rangeOfString:@"&rdquo;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\""];
    }
    while ((r = [s rangeOfString:@"&quot;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\""];
    }
    while ((r = [s rangeOfString:@"&#39;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\'"];
    }
    while ((r = [s rangeOfString:@"&lsquo;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\'"];
    }
    while ((r = [s rangeOfString:@"&rsquo;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"\'"];
    }
    while ((r = [s rangeOfString:@"&lt;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"<"];
    }
    while ((r = [s rangeOfString:@"&gt;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@">"];
    }
    while ((r = [s rangeOfString:@"&hellip;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"..."];
    }
    while ((r = [s rangeOfString:@"&amp;" options:NSRegularExpressionSearch]).location != NSNotFound) {
        s = [s stringByReplacingCharactersInRange:r withString:@"&"];
    }
    return s;
}

- (BOOL)report_isBlankString {
    if ([self isAllSpace]) {
        return NO;
    }
    else {
        return [self isBlankString];
    }
}
@end
